package com.xfcy.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xfcy.blog.common.R;
import com.xfcy.blog.common.constant.RedisConstant;
import com.xfcy.blog.common.constant.SqlConstant;
import com.xfcy.blog.dto.FeedBackDTO;
import com.xfcy.blog.mapper.FeedBackMapper;
import com.xfcy.blog.entity.FeedBack;
import com.xfcy.blog.service.FeedBackService;
import com.xfcy.blog.utils.BeanCopyUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * (FeedBack)表服务实现类
 *
 * @author 晓风残月Lx
 * @since 2023-04-09 11:03:05
 */
@Service("feedBackService")
public class FeedBackServiceImpl extends ServiceImpl<FeedBackMapper, FeedBack> implements FeedBackService {
    @Resource
    private RedisTemplate redisTemplate;


    /**
     * 后台反馈列表
     *
     * @param pageNo
     * @param pageSize
     * @param type
     * @return
     */
    @Override
    public R listFeedBack(Integer pageNo, Integer pageSize, Integer type) {
        Page<FeedBack> feedBackPage = baseMapper.selectPage(new Page<>(pageNo, pageSize), new QueryWrapper<FeedBack>().eq(type != null,SqlConstant.TYPE, type));
        return R.success(feedBackPage);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R deleteBatchFeedBack(List<Integer> ids) {
        int rows = baseMapper.deleteBatchIds(ids);
        return rows > 0 ? R.success(): R.error("删除反馈失败");
    }


    // Web方法 ======


    /**
     * 添加反馈
     * @param feedBackDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R insertFeedBack(FeedBackDTO feedBackDTO) {
        FeedBack feedBack = BeanCopyUtil.copyObject(feedBackDTO, FeedBack.class);
        feedBack.setCreateTime(LocalDateTime.now());
        int insert = baseMapper.insert(feedBack);

        // 存图片
//        redisTemplate.opsForSet().add(RedisConstant.DB_FILE_UPLOAD_IMAGES, feedBackDTO.getImgUrl());

        return insert > 0 ? R.success() : R.error("添加反馈失败");
    }
}


